package user

import (
	"log"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/resolver"
	"test.com/project-common/discovery"
	"test.com/project-common/logs"
	"test.com/project-api/config"
	loginServiceV1 "test.com/project-user/pkg/service/login.service.v1"
)

var LoginServiceClient loginServiceV1.LoginServiceClient

// init function to ensure the client is initialized when the package is imported
// func init() {
// 	initRpcUserClient()
// }

func initRpcUserClient(){
	// 	etcdRegister := discovery.NewResolver(config.C.EtcdConfig.Addrs, logs.LG)
	// resolver.Register(etcdRegister)

	//连接project-user 的 grpc 服务
	//这个地址时长变化    那么我们就的用服务发现 如etcd的地址     注册 project-user etcd地址 我们这个监听etcd地址
	//etcd 做一些负载均衡
	//conn, err := grpc.NewClient("127.0.0.1:8881", grpc.WithTransportCredentials(insecure.NewCredentials()))
	etcdRegister:= discovery.NewResolver(config.C.EtcdConfig.Addrs, logs.LG)
	resolver.Register(etcdRegister)
//配置里面是 user 是 grpc:  name: "user"
	conn, err := grpc.NewClient("etcd:///user", grpc.WithTransportCredentials(insecure.NewCredentials()))

	// conn, err := grpc.NewClient("127.0.0.1:8881", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	} 
	
	// Properly assign the client to the variable
	LoginServiceClient = loginServiceV1.NewLoginServiceClient(conn)
	// ProjectServiceClient = project.NewProjectServiceClient(conn)
}